/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.recordatorio;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.Recordatorio;
import clases.Usuario;
import java.util.ArrayList;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author jesus
 */
public class EliminarRecordatorio extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    private static final String name = "Eliminar Recordatorio";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1, where "method" is the value
     * specified in <action> element : ( <action parameter="method" .../> )
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();

//        DataBaseManager dbm = DataBaseManager.getInstance();
        ArrayList<Recordatorio> valores;
        Recordatorio recordatorioASerEliminado = (Recordatorio) form;
        //valores = dbm.consultarRecordatorios(recordatorioASerEliminado);
        //recordatorioASerEliminado= valores<0>;
        System.out.println(recordatorioASerEliminado.getFechaInicio() + " fechaI -fechaF " + recordatorioASerEliminado.getFechaFin());
        if (dbms.eliminarRecordatorio(recordatorioASerEliminado)) {
            recordatorioASerEliminado.setMensajeOK("Recordatorio eliminado"
                    + "con exito");
            request.setAttribute("recordatorio", recordatorioASerEliminado);
            valores = dbms.consultarRecordatorios(new Recordatorio());
            request.setAttribute("recordatorios", valores);
            return mapping.findForward(SUCCESS);
        } else {
            recordatorioASerEliminado.setMensaje("No se puedo eliminar el"
                    + "recordatorio.");
            request.setAttribute("recordatorio", recordatorioASerEliminado);

            valores = dbms.consultarRecordatorios(new Recordatorio());
            request.setAttribute("recordatorios", valores);
            return mapping.findForward(FAILURE);
        }
    }

    /*
    public ActionForward populate(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    
    return mapping.findForward(SUCCESS);
    }
     */
}
